package web.directories.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.*;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: scamisay                                                  fv
 * Date: 8/27/12
 * Time: 12:16 AM
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/Directories/Center")
public class CenterController extends ActionSupport implements ModelDriven {

    private CenterCRUDService service;
    private AddressService addressService;
    private TherapeuticAreaCRUDService therapeuticAreaCRUDService;
    private SpecialityCRUDService specialityCRUDService;

    private Centro centro = new Centro();
    private CentroAddress centerAddress = new CentroAddress();
    private List<Centro> list = new ArrayList<Centro>();
    private String editableCenterId;

    private List<Countries> countries = new ArrayList<Countries>();
    private List<Regions> regions = new ArrayList<Regions>();
    private List<Cities> cities = new ArrayList<Cities>();
    private Countries countrySelected;
    private Regions regionSelected;
    private Cities citySelected;
    private List<ComiteDocencia> listTeachingCommittee;
    private List<TherapeuticArea> therapeuticAreas;
    private List<Address> addresses;
    private String editableCenterAddressId;
    private LinkedHashMap<TherapeuticArea, ArrayList<Specialty>> languageMap;
    private List<Specialty> specialities;
    private TherapeuticArea therapeuticAreaLAF;
    private Specialty specialtyLAF;
    private Address address;

    @Autowired
    public CenterController(CenterCRUDService service, AddressService addressService,
                            TherapeuticAreaCRUDService therapeuticAreaCRUDService,
                            SpecialityCRUDService specialityCRUDService) {
        this.service = service;
        this.addressService = addressService;
        this.therapeuticAreaCRUDService = therapeuticAreaCRUDService;
        this.specialityCRUDService = specialityCRUDService;
        countries = this.addressService.getCountries();
    }

    @Action(value="main", results={
            @Result(name="success", location="/directories.center.tiles", type="tiles")
    })
    public String execute() {

        list = new ArrayList<Centro>(0);
        prepareAreaAndSpecialities();
        return SUCCESS;
    }

    @Action(value="list", results={
            @Result(name="success", location="/directories.center.tiles", type="tiles")
    })
    public String list() {
        List<CentroAddress> cas = new ArrayList<CentroAddress>();
        CentroAddress ca = new CentroAddress();
        if(therapeuticAreaLAF != null){
            TherapeuticArea ta = therapeuticAreaCRUDService.findById(therapeuticAreaLAF.getId());
            ca.setTherapeuticArea(ta);
        }

        if(specialtyLAF != null){
            Specialty s = specialityCRUDService.findById(specialtyLAF.getId());
            ca.setSpeciality(s);
        }

        ca.setAddress(address);
        cas.add(ca);
        centro.setCentroAddresses((List<CentroAddress>) cas);

        CriteriaCenter cr = new CriteriaCenter(centro);
        list = service.list(cr);

        prepareAreaAndSpecialities();

        return SUCCESS;
    }

    @Action(value="create", results={
            @Result(name="success",location="/directories.center.create.tiles", type="tiles")
    })
    public String create() {
        if(editableCenterId != null){
            int id = Integer.parseInt(editableCenterId);
            centro = service.findById(id);
            if(addresses == null)
                addresses = new ArrayList<Address>(0);
            addresses.clear();

            countries = addressService.getCountries();
            if(!centro.getCentroAddresses().isEmpty()){
                countrySelected = addressService.getCountryByName(centro.getCentroAddresses().iterator().next().getAddress().getCountry());
                regions = addressService.getRegions(countrySelected.getCountryId());
                regionSelected = addressService.getRegionByName(centro.getCentroAddresses().iterator().next().getAddress().getProvince(),
                        countrySelected.getCountryId().toString());
                cities = addressService.getCities(regionSelected.getRegionId());
            }

            ArrayList<CentroAddress> aux = new ArrayList<CentroAddress>(0);
            for (CentroAddress c : centro.getCentroAddresses()){
                if(!addresses.contains(c.getAddress())){
                    addresses.add(c.getAddress());
                }
                if(c.getSpeciality()== null && c.getTherapeuticArea()==null )
                    aux.add(c);
            }
            centro.getCentroAddresses().removeAll(aux);
        }

        prepareAreaAndSpecialities();

        countries = this.addressService.getCountries();
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String save() {
        service.save(centro, addresses);
        return SUCCESS;
    }

    @Action(value="delete", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String delete() {
        service.delete(service.findById(centro.getId()));
        return SUCCESS;
    }


    @Action(value="getSpecialities", results={
            @Result(name="success",location="/WEB-INF/jsp/Directories/center/specialities.jsp")
    })
    public String getSpecialitiesC() {
        therapeuticAreas = therapeuticAreaCRUDService.list();
        languageMap = new LinkedHashMap<TherapeuticArea, ArrayList<Specialty>>();

        for(TherapeuticArea t : therapeuticAreas){
            languageMap.put(t, new ArrayList<Specialty>(t.getSpecialities()));
        }
        return SUCCESS;
    }

    private void prepareAreaAndSpecialities(){
        ArrayList<TherapeuticArea> therapeuticAreas = new ArrayList<TherapeuticArea>(therapeuticAreaCRUDService.list());
        languageMap = new LinkedHashMap<TherapeuticArea, ArrayList<Specialty>>();
        specialities = new ArrayList<Specialty>(0);

        for(TherapeuticArea t : therapeuticAreas){
            specialities.addAll(t.getSpecialities());
        }
        TherapeuticArea t = new TherapeuticArea();
        t.setId(-1);
        t.setName("Choose an Area");
        t.setSpecialities(specialities);

        languageMap.put(t, new ArrayList<Specialty>(t.getSpecialities()));
        for(TherapeuticArea t2 : therapeuticAreas){
            languageMap.put(t2, new ArrayList<Specialty>(t2.getSpecialities()));
        }

    }

/*
    @Action(value="saveCenterAddress", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String saveContact(){
        centro = service.findById(centro.getId());
        TherapeuticArea therapeuticArea = therapeuticAreaCRUDService.findById(centerAddress.getTherapeuticArea().getId());
        centerAddress.setTherapeuticArea(therapeuticArea);
        service.saveCentroAddress(centro, centerAddress);
        return SUCCESS;
    }

    @Action(value="deleteCenterAddress", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String deleteCenterAddress(){
        centerAddress = service.findCenterAddressById(centerAddress.getId());
        service.deleteCentroAddress(centerAddress);
        return SUCCESS;
    }
*/

    @Override
    public Object getModel() {
        return centro;
    }

    public Centro getCentro() {
        return centro;
    }

    public void setCentro(Centro centro) {
        this.centro = centro;
    }

    public List<Centro> getList() {
        return list;
    }

    public void setList(List<Centro> list) {
        this.list = list;
    }

    public String getEditableCenterId() {
        return editableCenterId;
    }

    public void setEditableCenterId(String editableCenterId) {
        this.editableCenterId = editableCenterId;
    }

    public List<Countries> getCountries() {
        return countries;
    }

    public void setCountries(List<Countries> countries) {
        this.countries = countries;
    }

    public List<Regions> getRegions() {
        return regions;
    }

    public void setRegions(List<Regions> regions) {
        this.regions = regions;
    }

    public List<Cities> getCities() {
        return cities;
    }

    public void setCities(List<Cities> cities) {
        this.cities = cities;
    }

    public Countries getCountrySelected() {
        return countrySelected;
    }

    public void setCountrySelected(Countries countrySelected) {
        this.countrySelected = countrySelected;
    }

    public Regions getRegionSelected() {
        return regionSelected;
    }

    public void setRegionSelected(Regions regionSelected) {
        this.regionSelected = regionSelected;
    }

    public Cities getCitySelected() {
        return citySelected;
    }

    public void setCitySelected(Cities citySelected) {
        this.citySelected = citySelected;
    }

    public List<ComiteDocencia> getListTeachingCommittee() {
        centro = service.findById(centro.getId());
        return service.getAllTeachingCommittee(centro);
    }

    public void setListTeachingCommittee(List<ComiteDocencia> listTeachingCommittee) {
        this.listTeachingCommittee = listTeachingCommittee;
    }

    public CentroAddress getCenterAddress() {
        return centerAddress;
    }

    public void setCenterAddress(CentroAddress centerAddress) {
        this.centerAddress = centerAddress;
    }

    public List<TherapeuticArea> getTherapeuticAreas() {
        therapeuticAreas = this.therapeuticAreaCRUDService.list();
        return therapeuticAreas;
    }

    public void setTherapeuticAreas(List<TherapeuticArea> therapeuticAreas) {
        this.therapeuticAreas = therapeuticAreas;
    }

    public String getEditableCenterAddressId() {
        return editableCenterAddressId;
    }

    public void setEditableCenterAddressId(String editableCenterAddressId) {
        this.editableCenterAddressId = editableCenterAddressId;
    }

    public HashMap<TherapeuticArea, ArrayList<Specialty>> getLanguageMap() {
        return languageMap;
    }

    public void setLanguageMap(LinkedHashMap<TherapeuticArea, ArrayList<Specialty>> languageMap) {
        this.languageMap = languageMap;
    }

    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    public List<Specialty> getSpecialities() {
        return specialities;
    }

    public void setSpecialities(List<Specialty> specialities) {
        this.specialities = specialities;
    }

    public TherapeuticArea getTherapeuticAreaLAF() {
        return therapeuticAreaLAF;
    }

    public void setTherapeuticAreaLAF(TherapeuticArea therapeuticAreaLAF) {
        this.therapeuticAreaLAF = therapeuticAreaLAF;
    }

    public Specialty getSpecialtyLAF() {
        return specialtyLAF;
    }

    public void setSpecialtyLAF(Specialty specialtyLAF) {
        this.specialtyLAF = specialtyLAF;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}
